<?php
include_once('config.inc.php');
$result = array();

if (isset($_FILES['Filedata']) )
{
	$file = $_FILES['Filedata']['tmp_name'];
	$error = false;
	$size = false;
	/*
	*options:为了防止上传相同的文件名 覆盖问题   在文件名前增加时间标注 
	*autor:df
	*/
	//$temp_file=iconv("UTF-8","GBK",$_FILES['Filedata']['name']);
	$temp_file=$_FILES['Filedata']['name'];
	$_time=date("YmdHis",time());
	
	$toFileFloder = dirname(__FILE__)."/upload/".$_time."/";
	if(!is_dir($toFileFloder))
	{
		@mkdir($toFileFloder);
		@chmod($toFileFloder,0777);
	}
	$toFile=$toFileFloder.$temp_file;
	if( move_uploaded_file( $file, $toFile ) ){
		//$_SESSION["uploadfiles"][time()]=$_time."/".$temp_file;
		$filepath=dirname(__FILE__)."/upload/session_".GetIP().".log";
		$session = fopen($filepath, 'a');
		fputs($session,  $_time."/".$temp_file."\n");
		fclose($session);
		//@chmod( $filepath, 0777 );		
		@chmod( $toFile, 0755 );		
	}else{
		$error = "Move uploaded file fail!";
	}

	$addr = gethostbyaddr($_SERVER['REMOTE_ADDR']);

	$log = fopen('script.log', 'a');
	fputs($log, ($error ? 'FAILED' : 'SUCCESS') . ' - ' . preg_replace('/^[^.]+/', '***', $addr) . ": {$_FILES['Filedata']['name']} - {$_FILES['Filedata']['size']} byte\n" );
	fclose($log);

	if ($error)
	{
		$result['result'] = 'failed';
		$result['error'] = $error;
	}
	else
	{
		$result['result'] = 'success';
		//$result['size'] = "Uploaded an image ({$size['mime']}) with  {$size[0]}px/{$size[1]}px.";
		$result['size'] = "Uploaded an file ok.";
	}

}
else
{
	$result['result'] = 'error';
	$result['error'] = 'Missing file or internal error!';
}

if (!headers_sent() )
{
	header('Content-type: application/json');
}

echo json_encode($result);

?>
